
/**
 * @file pwm.c
 * @author      JinChengLong (https://gitee.com/Datejin/)
 * @brief       pwm库函数
 * @version     0.1
 * @date        2023
 * 
 * @copyright   Copyright © 2025 by JinChengLong 
 * 
 * All rights reserved.
 * Unauthorized copying, reproduction, or redistribution of this file via any medium is strictly prohibited.  
 * Proprietary and confidential.
 *   
 */
#include "pwm.h"

extern uint8_t num ;

/**
 * @brief 所有pwm初始化
 * 
 */
void pwm_init(void)
{

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);			//初始化PA0引脚
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);			//打开TIM2定时器时钟
	TIM_InternalClockConfig(TIM2);		//选择为72M内部时钟（默认内部时钟）
	
	
	
	//初始化TIM2通道
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;			//复用上拉输入用定时器控制引脚
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	
	//时基单元配置
	//定时频率=72M/（psc+1）/（ARR+1）
	//10k=10ms
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_ClockDivision =TIM_CKD_DIV1;		//TIM2 1分频
	TIM_TimeBaseInitStructure.TIM_CounterMode =TIM_CounterMode_Up;	//向上计数(从零到重装值）
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter =0;				//重复计数器的值（高级定时器）
	TIM_TimeBaseInitStructure.TIM_Prescaler	=72-1;						//PSC预分频器的值(0到65535）72M频率变为10kHz
	TIM_TimeBaseInitStructure.TIM_Period =20000-1;						//ARR自动重装器的值（周期）（0到65535）
																			//计算一秒的时间10k的频率计算10000个数，一秒一个周期
	TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
									
	//输出比较单元
	TIM_OCInitTypeDef TIM_OCInitSturcture;
	TIM_OCStructInit(&TIM_OCInitSturcture);		 			//结构体没有完全设置，为剩余部分赋予初始值
	TIM_OCInitSturcture.TIM_OCMode=TIM_OCMode_PWM1;			//设置为PWM模式一
	TIM_OCInitSturcture.TIM_OCPolarity=TIM_OCPolarity_High;	//	高极性，极性不翻转，REF直接输出，高电平有效
	TIM_OCInitSturcture.TIM_OutputState=TIM_OutputState_Enable;	//输出使能
	TIM_OCInitSturcture.TIM_Pulse=	0;						//CCR的值
	
	TIM_OC3Init(TIM2, &TIM_OCInitSturcture);//只能在TIM2CH2借用的PA1引脚
	
	
	
	
	
	//启动定时器
	TIM_Cmd(TIM2,ENABLE);									//定时器正常工作
	
}







